package com.food.restaurantpro.fragments;

import java.util.ArrayList;

import com.food.restaurantpro.R;
import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.activity.MainActivity;
import com.food.restaurantpro.adapters.YourPreOrderListViewAdapter;
import com.food.restaurantpro.objects.Food;
import com.food.restaurantpro.objects.PreOrderObject;
import com.food.restaurantpro.observers.CancelPreOrderObserver;
import com.food.restaurantpro.observers.CancelPreOrderObserverInterface;
import com.food.restaurantpro.observers.ListOrderAndDetailObserver;
import com.food.restaurantpro.observers.ListOrderAndDetailObserverInterface;
import com.food.restaurantpro.utils.AlertDialogInterface;
import com.food.restaurantpro.utils.AlertDialogManager;
import com.food.restaurantpro.utils.Utils;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class YourPreOrderFragment extends Fragment implements OnClickListener {

	private TextView tvHeader;
	private ListView mListView;
	private ArrayList<PreOrderObject> mPreOrderObjectList;
	private YourPreOrderListViewAdapter mYourPreOrderListViewAdapter;

	private Button btnMakePreOrder;
	
	private RelativeLayout rl_preorder_details;
	private RelativeLayout rl_preorder_notification;

	private ListOrderAndDetailObserver mListOrderAndDetailObserver;
	private CancelPreOrderObserver mCancelPreOrderObserver;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_your_pre_order,
				container, false);
		
		rl_preorder_details = (RelativeLayout) view.findViewById(R.id.rl_preorder_details);
		rl_preorder_notification = (RelativeLayout) view.findViewById(R.id.rl_preorder_notification);
		
		tvHeader = (TextView) view.findViewById(R.id.tvHeader);
		tvHeader.setText(R.string.tvPreOrder_Title);

		btnMakePreOrder = (Button) view.findViewById(R.id.btnMakePreOrder);
		
		btnMakePreOrder.setOnClickListener(this);
		

		mListView = (ListView) view.findViewById(R.id.listViewPreOder);
		// --------------------------
		
		if (!Utils.isEmpty(RestaurantApp.userInfo.getGenerateId())) {
			mListOrderAndDetailObserver = new ListOrderAndDetailObserver(new ListOrderAndDetailObserverInterface() {
				
				@Override
				public void listOrderAndDetailCallback(
						ArrayList<PreOrderObject> listPreOrderObject, String mesageError) {
					// TODO Auto-generated method stub
					if(mesageError != null) {
						AlertDialogManager.showAlertDialog(getActivity(), new AlertDialogInterface() {
							
							@Override
							public void onChooseYesCallback() {
								// TODO Auto-generated method stub
								
							}
							
							@Override
							public void onChooseNoCallback() {
								// TODO Auto-generated method stub
								
							}
						}, false, true, getActivity().getString(R.string.title_internet_error), mesageError, "OK", "", false);
					} else {
						if(listPreOrderObject != null && listPreOrderObject.size() > 0 ) {
							mPreOrderObjectList = listPreOrderObject;
							mYourPreOrderListViewAdapter = new YourPreOrderListViewAdapter(YourPreOrderFragment.this, mPreOrderObjectList);
							mListView.setAdapter(mYourPreOrderListViewAdapter);
						} else {
							rl_preorder_details.setVisibility(View.GONE);
							rl_preorder_notification.setVisibility(View.VISIBLE);
						}
					}
				}
			});
			mListOrderAndDetailObserver.registerObserver(getActivity(), RestaurantApp.userInfo.getGenerateId());
		} else {
			rl_preorder_details.setVisibility(View.GONE);
			rl_preorder_notification.setVisibility(View.VISIBLE);
		}
		return view;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnPreOrder:
		case R.id.btnMakePreOrder:
			goToMakingPreOrder();
			break;
		default:
			break;
		}
	}

	private void goToMakingPreOrder() {
		AlertDialogManager.showCustomDialog(getActivity(), new AlertDialogInterface() {
			
			@Override
			public void onChooseYesCallback() {
				// TODO Auto-generated method stub
				((MainActivity)getActivity()).showMenuFragment();
			}
			
			@Override
			public void onChooseNoCallback() {
				// TODO Auto-generated method stub
				
			}
		}, "Making new Pre-order", "Do you want to make new Pre-order?", "Yes", "No", true);
	}
	
	@Override
	public void onDestroy() {
		// TODO delete image and cache de giai phong bo nho
		super.onDestroy();
		//
		mListView.setAdapter(null);
		if (mYourPreOrderListViewAdapter != null) {
			mYourPreOrderListViewAdapter.clearMemoryCache();
			mYourPreOrderListViewAdapter = null;
		}
		//
		if (mPreOrderObjectList != null) {
			mPreOrderObjectList.clear();
			mPreOrderObjectList = null;
		}
		if(mListOrderAndDetailObserver != null) {
			mListOrderAndDetailObserver.unRegisterObserver();
			mListOrderAndDetailObserver = null;
		}
		if(mCancelPreOrderObserver != null) {
			mCancelPreOrderObserver.unRegisterObserver();
			mCancelPreOrderObserver = null;
		}
		Runtime.getRuntime().gc();
	}

	/**
	 * Remove item in list pre-order
	 * @param position
	 */
	private void gotoCancelPreOrder(int position) {
		// TODO Auto-generated method stub
		if(mCancelPreOrderObserver == null) {
			mCancelPreOrderObserver = new CancelPreOrderObserver(new CancelPreOrderObserverInterface() {
				
				@Override
				public void cancelPreOrderCallback(int position, String messageError) {
					// TODO Auto-generated method stub
					if(messageError != null) {
						AlertDialogManager.showAlertDialog(getActivity(), new AlertDialogInterface() {
							
							@Override
							public void onChooseYesCallback() {
								// TODO Auto-generated method stub
								
							}
							
							@Override
							public void onChooseNoCallback() {
								// TODO Auto-generated method stub
								
							}
						}, false, true, getActivity().getString(R.string.title_internet_error), messageError, "OK", "", false);
					} else {
						
						mListView.setAdapter(null);
						
						mPreOrderObjectList.remove(position);
						if(mPreOrderObjectList.size() > 0) {
							mYourPreOrderListViewAdapter = null;
							mYourPreOrderListViewAdapter = new YourPreOrderListViewAdapter(YourPreOrderFragment.this, mPreOrderObjectList);
							mListView.setAdapter(mYourPreOrderListViewAdapter);
						} else {
							mYourPreOrderListViewAdapter.clearMemoryCache();
							mYourPreOrderListViewAdapter = null;
							rl_preorder_details.setVisibility(View.GONE);
							rl_preorder_notification.setVisibility(View.VISIBLE);
						}
					}
				}
			});
		}
		mCancelPreOrderObserver.registerObserver(getActivity(), mPreOrderObjectList.get(position).getPreorderID(), position);
		
	}
	
	public void showDialogCancelPreOrder(final int position) {
		AlertDialogManager.showCustomDialog(getActivity(), new AlertDialogInterface() {
			
			@Override
			public void onChooseYesCallback() {
				// TODO Auto-generated method stub
				gotoCancelPreOrder(position);
			}
			
			@Override
			public void onChooseNoCallback() {
				// TODO Auto-generated method stub
				
			}
		}, "Cancellation of order", "Do you want to cancel this order?", "Yes", "No", true);
	}

	public void showDialogEditPreOrder(final int which) {
		// TODO Auto-generated method stub
		AlertDialogManager.showCustomDialog(getActivity(), new AlertDialogInterface() {
			
			@Override
			public void onChooseYesCallback() {
				// TODO Auto-generated method stub
				gotoEditPreOrder(which);
			}
			
			@Override
			public void onChooseNoCallback() {
				// TODO Auto-generated method stub
				
			}
		}, "Update Pre-Order", "Are you sure want to update this order?", "Yes", "No", true);
	}
	
	private void gotoEditPreOrder(int position) {
		RestaurantApp.getInstance().setPreOrderObject(mPreOrderObjectList.get(position));
		((MainActivity)getActivity()).showMenuPreOrderFragment();
	}
}
